package com.rbac.utils;

import io.lettuce.core.RedisCommandTimeoutException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.stereotype.Component;

@Component
public class LettuceConnectionValidConfig implements InitializingBean {
    private final RedisConnectionFactory redisConnectionFactory;

    public LettuceConnectionValidConfig(RedisConnectionFactory redisConnectionFactory) {
        this.redisConnectionFactory = redisConnectionFactory;
    }

    @Override
    public void afterPropertiesSet() throws RedisCommandTimeoutException {
        if (redisConnectionFactory instanceof LettuceConnectionFactory) {
            LettuceConnectionFactory c = (LettuceConnectionFactory) redisConnectionFactory;
            c.setValidateConnection(true);
        }
    }
}